﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.BLL.NetBar;
using FortuneHeaven.BLL.Accounts;
using FortuneHeaven.Entity.NetBar;
using FortuneHeaven.Entity.Accounts;
using FortuneHeaven.BLL.GameScore;
using Jzy.Core.Utility;
using Jzy.Common;
using Jzy.Core.Log.Exceptions;

namespace FortuneHeaven.Admin.Pages.Accounts
{
    public partial class AddScore : PageEdit
    {
        private NetBarBO NetBar
        {
            get
            {
                return NetBarBO.Instance<NetBarBO>();
            }
        }

        private ScoreHistoryBO ScoreHistory
        {
            get
            {
                return ScoreHistoryBO.Instance<ScoreHistoryBO>();
            }
        }

        private GameScoreBO GameScore
        {
            get
            {
                return GameScoreBO.Instance<GameScoreBO>();
            }
        }
      

        private string AccountID
        {
            get
            {
                return Request.QueryString["AccountID"];
            }
        }

        private string NetBarID
        {
            get
            {
                return Request.QueryString["NetBarID"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AccountsBO account = new AccountsBO();
                AccountsInfo info = account.GetInfo(AccountID);
                litAccounts.Text = info.Accounts;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            long amount = long.Parse(txtAmount.Text);

            if (amount <= 0)
            {
                Alert("积分要为正数");
                return;
            }

            if (amount % 100 > 0)
            {
                Alert("请输入100的倍数");
                return;
            }

            try
            {

                NetBar.dbOper.BeginTrans();
                ScoreHistory.dbOper.BeginTrans();
                GameScore.dbOper.BeginTrans();                


                NetBarInfo info = NetBar.GetInfoWithLock(int.Parse(NetBarID));
                if (info.Score < amount)
                {
                    Alert("您网吧的积分不足，请充值");
                }
                else
                {
                    ScoreHistoryInfo historyInfo = new ScoreHistoryInfo();

                    long accountRemain = this.GameScore.GetScoreWithLock(int.Parse(AccountID));

                    historyInfo.Score = amount;                   
                    historyInfo.InputTime = DateTime.Now;
                    historyInfo.NetBarID = int.Parse(NetBarID);
                    historyInfo.NetBarName = info.NetbarName;
                    historyInfo.UserID = AppSession.UserID;
                    historyInfo.UserName = AppSession.UserName;
                    historyInfo.Remark = txtRemark.Text;
                    historyInfo.NetBarRemain = info.Score;
                    historyInfo.AccountRemain = accountRemain;
                    historyInfo.AccountID = int.Parse(AccountID);
                    historyInfo.AccountName = litAccounts.Text;
                 

                    info.Score -= historyInfo.Score;
                    accountRemain += historyInfo.Score;                   

                    NetBar.Save(info);
                    GameScore.UpdateScore(int.Parse(AccountID), accountRemain);
                    ScoreHistory.Save(historyInfo);

                }

                NetBar.dbOper.CommitTrans();
                ScoreHistory.dbOper.CommitTrans();
                GameScore.dbOper.CommitTrans();
                JSUtil.RefreshView();
            }
            catch (Exception ex)
            {
                Alert("充值失败");
                LogExceptionBO.Save(ex);
                NetBar.dbOper.RollbackTrans();
                ScoreHistory.dbOper.RollbackTrans();
            }
        }
    }
}